package com.itcanteen.reggie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcanteen.reggie.entity.Category;
import com.itcanteen.reggie.entity.Dish;
import com.itcanteen.reggie.exception.CustomerException;
import com.itcanteen.reggie.exception.NameExistsException;
import com.itcanteen.reggie.mapper.CategoryDao;
import com.itcanteen.reggie.mapper.DishDao;
import com.itcanteen.reggie.mapper.SetmealDao;
import com.itcanteen.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author baimugudu
 * @email 2415621370@qq.com
 * @date 2022/10/25 15:35
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {


    @Autowired(required = false)
    CategoryDao categoryDao;

    @Autowired(required = false)
    SetmealDao setmealDao;

    @Autowired(required = false)
    DishDao dishDao;

    @Override
    public void save(Category category) {

        QueryWrapper<Category> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("name",category.getName());
        Category categoryDB = categoryDao.selectOne(objectQueryWrapper);

        if(null!=categoryDB){
            throw  new NameExistsException(categoryDB.getName());
        }
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        categoryDao.insert(category);
    }

    @Override
    public Page<Category> findByPage(Integer page, Integer pageSize) {

        Page<Category> pageInfo = new Page<>(page, pageSize);

        QueryWrapper<Category> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.orderByAsc("sort");
        return categoryDao.selectPage(pageInfo,objectQueryWrapper);
    }

    /**
     * 根据id删除类别
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        QueryWrapper dishQueryWrapper = new QueryWrapper<>();
        dishQueryWrapper.select("count(*)");
        dishQueryWrapper.eq("category_id",id);
        List<Map<String, Long>> maps = dishDao.selectMaps(dishQueryWrapper);
        log.info("this is maps:{}",JSON.toJSONString(maps));

        if(!maps.get(0).get("count(*)").equals(0L)){
            throw new CustomerException("该类别已经关联菜品不能直接删除");
        }

        List<Map<String, Object>> map = setmealDao.selectMaps(dishQueryWrapper);

        log.info("this is maps:{}",JSON.toJSONString(map));
        if(!map.get(0).get("count(*)").equals(0L)){
            throw new CustomerException("该类别已经关联其他类别不能直接删除");
        }
        categoryDao.deleteById(id);

    }

    @Override
    public void update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<Category> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.set(Category::getName,category.getName());
        updateWrapper.set(Category::getSort,category.getSort());
        updateWrapper.set(Category::getUpdateTime,category.getUpdateTime());
        updateWrapper.set(Category::getUpdateUser,category.getUpdateUser());
        updateWrapper.eq(Category::getId,category.getId());
        categoryDao.update(category,updateWrapper);
    }

    @Override
    public List<Category> findAll() {
        return categoryDao.selectList(null);
    }

    @Override
    public List<Category> list(Integer type) {
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getType,type);
        return categoryDao.selectList(queryWrapper);
    }
}
